<template>
  <el-dialog :title="FormList.id?'编辑客户':'添加客户'" :visible="showDialog" @close="closeFn">
    <el-form ref="form" :rules="rules" :model="FormList" label-width="100px">
      <el-form-item label="经纪">
        <el-select v-model="value" placeholder="select" @change="agentChange">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="FormList.name" />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="FormList.email" />
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="FormList.phone" />
      </el-form-item>
      <el-form-item label="微信">
        <el-input v-model="FormList.wechat" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="FormList.desc" />
      </el-form-item>
      <el-form-item label="封面" prop="image">
        <!-- 放置上传图片 -->
        <UploadImage ref="img1" />
        <el-input placeholder="强制设置图片跳过上传" />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="end">
        <el-button size="small" @click="etiFn">取消</el-button>
        <el-button size="small" type="primary" @click="submit">确定</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addcustomerList, customerDetail, editCustomer, getJingJi } from '@/api/kehu'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      FormList: {
        agent: 0, // 属于哪个经纪的 id 值
        name: '', // 名称
        email: '', // 电子邮箱，比如 example@qq.com
        phone: '', // 电话
        wechat: '', // 微信
        desc: '', // 描述
        image: '' // 图片
      }, // 表单数据
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      },
      options: [{
        value: '',
        label: ''
      }],
      value: ''
    }
  },
  created() {
    this.getJingJi()
  },
  methods: {
    agentChange(id) {
      console.log(id)
      this.FormList.agent = id
    },
    // 获取经纪列表
    async getJingJi() {
      const res = await getJingJi({
        _limit: this.limit,
        _start: this.start

      })
      this.options = res.data
    },
    // 获取客户详情 实现客户信息回显
    async getcustomerDetail(id) {
      const res = await customerDetail(id)
      this.FormList = res.data
    },
    // 新增 ||编辑
    async submit() {
      // 表单校验
      await this.$refs.form.validate()
      this.FormList.image = this.$refs.img1.imgUrl
      if (this.FormList.id) {
        // 编辑数据
        await editCustomer(this.FormList)
      } else {
        // 新增数据  调用接口
        console.log('新增')
        await addcustomerList(this.FormList)
      }
      // 关闭弹窗
      this.closeFn()
      // 更新页面
      this.$emit('updateEvent')
    },
    // 取消新增
    etiFn() {
      this.closeFn()
    },
    closeFn() {
      this.$emit('update:showDialog', false)
      // 清空校验规则
      this.$refs.form.resetFields()
      // 清空数据
      this.FormList = {
        agent: '', // 属于哪个经纪的 id 值
        name: '', // 名称
        email: '', // 电子邮箱，比如 example@qq.com
        phone: '', // 电话
        wechat: '', // 微信
        desc: '', // 描述
        image: '' // 图片
      }
    }
  }

}
</script>

<style>

</style>
